//
//  main.c
//  C_15文件操作
//
//  Created by Robin on 16/3/28.
//  Copyright © 2016年 cnbm. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

void error (char *s){
    printf("%s:%s\n",s,strerror(errno));
    exit(1);
}

void readDistData (){
    
    FILE *file;
    char ch;
    
//    printf("Please input filename:\n");
//    //gets(filename);
//    fgets(filename, 50, stdin);
    file = fopen("/Users/guorobin/Desktop/C.txt", "r");
    //ch = fgetc(file);
    ///Users/guorobin/Desktop/My Git/C/C-Learning/C_15文件操作/C_15文件操作/main.c
    while ((ch = fgetc(file)) != EOF) {
        putchar(ch);
        //printf("\n");
    }
    
    fclose(file);
}

// 格式化读写文件
void FormattingWR (){
    
    int i,flag = 1;
    FILE *fp;
    char filename[50],filecontent[80];
    printf("please input you file's name:\n");
    scanf("%s",filename);
    fp = fopen(filename, "w");
    while (flag) {
        printf("\ninput your string:\n");
        scanf("%s",filecontent);
        fprintf(fp, "%s",filecontent);
        printf("continue:\n");
        
        if ((getchar() == 'N') || (getchar() == 'n') )
            flag = 0;
        
    }
    
    fclose(fp);
    
    fp = fopen(filename, "r");

    while (fscanf(fp, "%s",filecontent) != EOF) {
        
        for (i=0; filecontent[i] != '\0'; i++) {
            if (filecontent[i]<'z'&&filecontent[i]>'a') {
                filecontent[i] -= 32;
                printf("%s\n",filecontent);
            }
        }
        
        
        fclose(fp);
    }
}

void writeDisk(){
    
    FILE *fp;
    char ch,filename[50];
    printf("please input file's name: \n");
    scanf("%s",filename);
    
    if ((fp = fopen(filename, "w")) == NULL) {
        puts("cannot open the file \n");
        error("不能打开文件");
        //exit(0);
    }
    
    while ((ch = getchar()) != '#') {
        fputc(ch, fp);
    }
    
    fclose(fp);
}

struct student {
    char name[5];
    char sex[4];
    int num;
    int math;
    int china;
    int english;
}score[100];

void save (char *name,int n){
    FILE *fp;
    int i;
    if ((fp = fopen(name, "wb")) == NULL) {
        error("cannot open the file");
    }
    /*
     C语言函数，向文件写入一个数据块
     注意：这个函数以二进制形式对文件进行操作，不局限于文本文件
     
     size_t fwrite(const void* buffer, size_t size, size_t count, FILE* stream);
     
     （1）buffer：是一个指针，对fwrite来说，是要获取数据的地址；
     （2）size：要写入内容的单字节数；
     （3）count:要进行写入size字节的数据项的个数；
     （4）stream:目标文件指针；
     （5）返回实际写入的数据项个数count。
     **/
    
    for (i=0; i<=n; i++) {
        if (fwrite(&score[i], sizeof(struct student), 1, fp) != 1) {
            error("file write error");
        }
    }
    
    fclose(fp);
}

void show (char *name,int n){
    
    int i;
    FILE *fp;
    if ((fp = fopen(name, "rb")) == NULL) {
        error("cannot open the file");
    }
    
    for (i=0; i<n; i++) {
        fread(&score[i], sizeof(struct student), 1, fp);
        printf("%-10s%4d%4d%4d%4d\n",score[i].name,score[i].num,score[i].math,score[i].china,score[i].english);
    }
    fclose(fp);
}

void blockInputOutPut(){
    
    int i=0,n;
    char filename[50] = {"/Users/guorobin/Desktop/C.txt"};
    
    printf("你们班有多少学生:\n");
    scanf("%d",&n);
    
    printf("请输入学生的姓名,性别,学号,数学,语文,英语成绩\n");
    
    for (i=0; i<n; i++) {
        printf("No%d\n",i+1);
        
        scanf("%s %s %d %d %d %d",score[i].name,score[i].sex,&score[i].num,&score[i].math,&score[i].china,&score[i].english);
        save(filename, i);
    }
    
    show(filename, n);
    
}

int main(int argc, const char * argv[]) {
    // insert code here...
    
    //readDistData();
    //writeDisk();
    //FormattingWR();
    blockInputOutPut();
    
    return 0;
}
